<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="后台登录"></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" th:href="@{/css/admin.css}">
    <script th:src="@{/js/jquery-3.4.1.min.js}"></script>
    <script th:src="@{/layui/layui.js}" type="application/javascript"></script>
    <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
    <script type="application/javascript">
        function adminLoginBtn() {
            var username = $("#username").val();
            var password = $("#password").val();
            layui.use('layer',function () {
                var layer = layui.layer;
                if(!username){
                    layer.msg("请输入用户名");
                }else if(!password){
                    layer.msg("请输入密码");
                }else{
                    var captcha1 = new TencentCaptcha('2077491291', function(res) {
                        if(res.ret == 0){
                            $.ajax({
                                type:"post",
                                url:"/checkCaptcha",
                                contentType:"application/json",
                                data:JSON.stringify({
                                    "ticket":res.ticket,
                                    "randstr":res.randstr
                                }),
                                success:function (result) {
                                    if(result.code == 200){
                                        $.ajax({
                                            type:"post",
                                            url:"/adminLogin",
                                            contentType:"application/json",
                                            data:JSON.stringify({
                                                "phone":username,
                                                "password":password
                                            }),
                                            success:function (result) {
                                                if(result.code == 200){
                                                    window.location.href="/admin/index";
                                                }else{
                                                    layer.msg(result.message);
                                                }
                                            },
                                            dataType:"json"
                                        });
                                    }
                                },
                                dataType:"json"
                            });
                        }
                    });
                    captcha1.show(); // 显示验证码
                }
            });
        }
    </script>
</head>
<body class="background-image">
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="adminLogin">
                <h2>树洞社区后台登录</h2>
                <div class="inputbox">
                    <input type="text" id="username" placeholder="用户名" autocomplete="off">
                </div>
                <div class="inputbox">
                    <input type="password" id="password" placeholder="密码" autocomplete="off">
                </div>
                <button class="layui-btn" onclick="adminLoginBtn()">登录</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>